//
//  MyopiaReserveViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/1/5.
//  Copyright © 2021 QDSG. All rights reserved.
//

import Foundation
import HandyJSON

enum MyopiaReserveCellType {
    case text
    case button
    
    var hiddenArrow: Bool {
        switch self {
        case .text:
            return false
        case .button:
            return true
        }
    }
    
    var hiddenText: Bool {
        switch self {
        case .text:
            return false
        case .button:
            return true
        }
    }
    
    var hiddenButton: Bool {
        switch self {
        case .text:
            return true
        case .button:
            return false
        }
    }
}

enum MyopiaReserveInputType {
    case scan
    case hand
}

struct MyopiaReserveModel: HandyJSON {
    var checkList: String = "" //检查列表code码拼接eg:101,102

    var checkSeries: String = "双眼" //检查位置：左眼，右眼，双眼

    var hosDevId: String = "" //设备编号

    var checkFee: String = "" //价格

    var applyHosid: String = "" //申请医院id

    var applyHosname: String = "" //申请医院名称

    var applyPatid: String = "" //申请患者id

    var applyPatname: String = "" //申请患者名称

    var applyPatidcard: String = "" //申请患者身份证

    var applyCheckTime: String = "" //申请检查时间，精确到时

    var source: String = "" //申请来源（0扫码、1预约）
}

struct MyopiaReserveViewModel {
    var model: MyopiaReserveModel
    
    // 医院支持检查项数组
    var checkProjectModelAry: [CheckProjectModel] = []
    
    // 选中检查项
    var checkProjectSelected: (code: String, name: String) {
        var codeArray = [String]();
        var nameArray = [String]();
        for model in checkProjectModelAry {
            if model.selected {
                codeArray.append(model.code)
                nameArray.append(model.name)
            }
        }
        return (codeArray.joined(separator: ","), nameArray.joined(separator: ";"))
    }
    
    // 提交数据校验
    func submitAvailable() -> String {
        if model.applyHosid.isEmpty {
            return "请选择检查医院"
        }
        if model.checkSeries.isEmpty {
            return "请选择检查眼别"
        }
        if model.applyPatid.isEmpty {
            return "请选择就诊人"
        }
        if checkProjectSelected.code.isEmpty {
            return "请选择检查项目"
        }
        if model.applyCheckTime.isEmpty {
            return "请选择检查时间"
        }
        return ""
    }
    
    var numberOfSection: Int {
        return titleForCellAry.count
    }
    
    func numberOfRows(section: Int) -> Int {
        switch section {
        case 0:
            return 3
        case 1:
            return checkProjectModelAry.count
        default:
            return 1
        }
    }
    
    func heightForHeader(section: Int) -> CGFloat {
        switch section {
        case 1:
            return checkProjectModelAry.isEmpty ? 0 : 49
        default:
            return 10
        }
    }
    
    func reserveCellType(indexPath: IndexPath) -> MyopiaReserveCellType {
        switch indexPath.section {
        case 1:
            return .button
        default:
            return .text
        }
    }
    
    var titleForCellAry: [[String]] {
        return [
            ["选择检查医院", "检查眼别", "选择就诊人"],
            titleForCheckProjectCell,
            ["选择检查时间"]
        ]
    }
    
    var titleForCheckProjectCell: [String] {
        var ary = [String]()
        for model in checkProjectModelAry {
            ary.append(model.name)
        }
        return ary
    }
    
    func titleForCell(indexPath: IndexPath) -> String {
        return titleForCellAry[indexPath.section][indexPath.row]
    }
    
    func memoForCell(indexPath: IndexPath) -> String {
        switch indexPath.section {
        case 0:
            switch indexPath.row {
            case 0:
                return model.applyHosname
            case 1:
                return model.checkSeries
            case 2:
                return model.applyPatname
            default:
                return ""
            }
        case 2:
            switch indexPath.row {
            case 0:
                return model.applyCheckTime
            default:
                return ""
            }
        default:
            return ""
        }
    }
    
    func selectedImgForCell(indexPath: IndexPath) -> String {
        guard reserveCellType(indexPath: indexPath) == .button, indexPath.row < checkProjectModelAry.count else {
            return ""
        }
        let model = checkProjectModelAry[indexPath.row]
        return model.selected ? "pay_circle_fill" : "pay_circle"
    }
    
    func cornersForCell(indexPath: IndexPath) -> UIRectCorner? {
        let ary = titleForCellAry[indexPath.section]
        if ary.count == 0 {
            return nil
        }
        if ary.count == 1 {
            return .allCorners
        }
        if indexPath.row == 0  {
            return [.topLeft, .topRight]
        }
        if indexPath.row == ary.count - 1 {
            return [.bottomLeft, .bottomRight]
        }
        return nil
    }
    
    static func submitMyopisReserve(vm: MyopiaReserveViewModel,
                                   finished: @escaping (_ msg: String?, _ code: HttpCode) -> Void) {
        let paras = [
            "checkList" : vm.checkProjectSelected.code,
            "checkSeries" : vm.model.checkSeries,
            "hosDevId" : vm.model.hosDevId,
            "applyHosid" : vm.model.applyHosid,
            "applyHosname" : vm.model.applyHosname,
            "applyPatid" : vm.model.applyPatid,
            "applyCheckTime" : vm.model.applyCheckTime,
            "source" : vm.model.source
        ]
        YSGLog(paras)
        HttpTool.shared.request(target: ApiManager.submitMyopiaReserve(paras: paras), success: { (json, code) in
            finished(json["message"].description, code)
        }) { errorMsg in
            finished(errorMsg, .failure)
        }
    }
}
